package com.cn.goodsadmin.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.goodsadmin.common.PageInfo;
import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.po.App;
import com.cn.goodsadmin.po.UserInfo;
import com.cn.goodsadmin.po.UserWordsInfo;
import com.cn.goodsadmin.service.AppService;
import com.cn.goodsadmin.service.UserService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    AppService appService;

    @RequestMapping(value = "/login", method = RequestMethod.POST, params = {"name", "password"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap login(HttpServletRequest request, HttpServletResponse response, String name, String password) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            App user = appService.login(name, password);
            if (user != null) {
                //保存
                HttpSession session = request.getSession(true);
                String pass="********";
                user.setcAppid(pass);
                user.setcPaykey(pass);
                user.setcSecretkey(pass);
                user.setcApppass(pass);
                session.setAttribute("user", user);
                supplyMap.put("user", user);
            } else {
                supplyMap.setEorrInfo(ErrorMsg.STATUS_ILLEGAL_USER, ErrorMsg.MSG_ILLEGAL_USER);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        System.out.println(JSONObject.toJSONString(supplyMap));
        return supplyMap;
    }


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap logout(HttpServletRequest request) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            HttpSession session = request.getSession(false);
            if(session!=null){
                session.invalidate();
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        System.out.println(JSONObject.toJSONString(supplyMap));
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/updateapp", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateUser(@RequestBody App user) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            appService.updateApp(user.getcSoreName(),user.getcSoreImg(),user.getcOrcode(),user.getcId());
            App app=appService.findAppById(user.getcId());
            if(app!=null){
                supplyMap.put("app",app);
            }
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/alluser", method = RequestMethod.GET, params = {"pageindex", "pagesize","appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap allUser(int pageindex, int pagesize,int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            PageQueryResult<DestDTO> pageQueryResult = userService.getAllUserInfo(pageindex, pagesize,appid);
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageQueryResult(pageQueryResult);
            supplyMap.put("page_info", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    /**
     *
     * @param openid
     * @param vip
     * @param sore
     * @return
     */
    @LoginRequired
    @RequestMapping(value = "/updateuser", method = RequestMethod.GET,params = {"openid","vip","sore","appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateUserInfo(String openid,int vip, int sore,int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            UserInfo userInfo=userService.updateUserInfo(openid,vip,sore,appid);
            if(userInfo!=null){
                supplyMap.put("userinfo",userInfo);
            }else{
                supplyMap.setEorrInfo(ErrorMsg.STATUS_INVALID_PARAMETERS,ErrorMsg.MSG_INVALID_PARAMETERS);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/userinfo", method = RequestMethod.GET, params = {"openid", "appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap allUser(String openid,int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            UserWordsInfo userWordsInfo=userService.getUserInfoByOpenid(openid,appid);
            supplyMap.put("userWordsInfo",userWordsInfo);
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
